/*var result =sessionStorage.getItem("resufflt")
var resultdsf =JSON.parse(result)
console.log(resultdsf)*/
var th={}
var customerid2=sessionStorage.getItem("customerid2")
intuy();
function intuy() {
    $.ajax({
        /*root.inter.findProductInfoList*/
        /*"httphttp://192.168.65.118:7050/aproduct/product/findProductInfoList"*/
        url:root.inter.findProductInfoList,
        contentType: "application/json;charset=UTF-8",
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data: JSON.stringify({
            "nameParam":sessionStorage.getItem("spuName"),
            "code":sessionStorage.getItem("skuCode"),//商品别名
            "helpcode":sessionStorage.getItem("helpCode"),//助记码
            "spuCode":sessionStorage.getItem("spuCode")
        }),
        success: function (result) {
            var handStr = '';
            if(result.data && result.data.length>0){
                $.each(result.data,function(k,v){
                    handStr+='<li class="pd-10" style="border-bottom: 1px dotted #dedede">'+
                        '<div>' +
                        '<span>'+v.spu_code+'</span>' +
                        '<span class="ml-80">品牌：</span><span class="pl-10">'+v.brand_name+'</span>' +
                        '<span class="ml-80">售卖信息：</span><span class="pl-10">'+v.area_name+'</span>'+
                        '<span class="ml-80 hidden">'+v.ongCategoryId+'</span>'+
                        '<span class="ml-80 hidden">'+v.brand_id+'</span>'+
                        '<span class="ml-80 hidden">'+v.towCategoryId+'</span>'+
                        '<span class="ml-80 hidden">'+v.threeCategoryId+'</span>'+
                        '<span class="ml-80 hidden">'+v.skuIds+'</span>'+
                        '<span class="ml-80 hidden">'+v.threeCategoryName+'</span>'+
                        '<span class="ml-80 hidden">'+v.sale_area_id+'</span>'+
                        '<span class="ml-80 hidden">'+v.ongCategoryName+'</span>'+
                        '<span class="ml-80 hidden">'+v.towCategoryName+'</span>'+
                        ' </div>'+
                        '<p class="mt-10">'+v.name+'</p>'+
                        '<div>'+
                        '<span>商品数：</span><span class="ml-10">'+v.sku_number+'</span>'+
                        '<span class="ml-80">已选数量：</span><span class="select_num ml-10"></span>'+
                        '<span class="f-r" onclick="spugoodss(this)">>></span>'+
                        '</div>'+
                        '</li>'
                });
            }
            $(".goods_spu").html(handStr)

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });

}
var skuValueCode = [];
var spuValueCode = [];
function spugoodss(el) {
    th = el;
    $("#spugoods").empty();
    var skuIds=th.parentElement.previousElementSibling.previousElementSibling.children[9].innerHTML
    var sale_area_id=th.parentElement.previousElementSibling.previousElementSibling.children[11].innerHTML
    var spucodes = el.parentElement.previousElementSibling.previousElementSibling.firstElementChild.innerHTML;
    var sale_area_id=th.parentElement.previousElementSibling.previousElementSibling.children[11].innerHTML
    $.ajax({
        url: root.inter.findSkuDatas,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        async:false,
        data: {
            "skuIds": skuIds,
            "sale_area_id":sale_area_id

        },
        success: function (result) {

            $(".select_sku").css({
                display: "inline"
            });
            //获取spu信息赋值
            var oneCategoryId=th.parentElement.previousElementSibling.previousElementSibling.children[5].innerHTML
            var twoCategoryId=th.parentElement.previousElementSibling.previousElementSibling.children[7].innerHTML
            var threeCategoryId=th.parentElement.previousElementSibling.previousElementSibling.children[8].innerHTML
            var threeCategoryname=th.parentElement.previousElementSibling.previousElementSibling.children[10].innerHTML
            var brandId=th.parentElement.previousElementSibling.previousElementSibling.children[6].innerHTML
            var brand = th.parentElement.previousElementSibling.previousElementSibling.children[2].innerHTML;
            var spucodes = th.parentElement.previousElementSibling.previousElementSibling.firstElementChild.innerHTML;
            var productName = th.parentElement.previousElementSibling.innerHTML;
            var ongCategoryName=th.parentElement.previousElementSibling.previousElementSibling.children[12].innerHTML;
            var towCategoryName=th.parentElement.previousElementSibling.previousElementSibling.children[13].innerHTML;
            var hangfhStr = '';
            var spuhanfhstr=""
            spuhanfhstr='<input type="button" class="btn  radius" id="'+oneCategoryId+','+twoCategoryId+','+threeCategoryId+','+threeCategoryname+','+brandId+','+brand+','+spucodes+','+productName+','+ongCategoryName+','+towCategoryName+'" value="该SPU">'
            if(result.data && result.data.length>0){
                $.each(result.data,function(k,v){
                    console.log(v)
                    hangfhStr += '<input type="button" id="'+ v.sku_code +','+oneCategoryId+','+twoCategoryId+','+threeCategoryId+','+threeCategoryname+','+brandId+','+brand+','+spucodes+','+productName+','+ongCategoryName+','+towCategoryName+','+v.helpcode+','+v.unit_name+','+v.sku_id+'" name="' + v.product_code + '" class="btn mr-10 mb-10 radius" value="' + v.specifications + '' + v.unit_name + '' + v.color + '">';

                });
            }
            $("#spuusd").html(spuhanfhstr);
            $("#spugoods").html(hangfhStr);

            var count = 0;
            $(function () {
                //获取sku
                $("#spugoods input").each(function (i) {
                    // console.log(count);
                    $(this).click(function () {
                        var oneSkuValueCode = {};
                        $(this).toggleClass("btn-primary");//添加颜色属性
                        if ($(this).hasClass("btn-primary")) {
                            console.log($(this).attr("id"));
                            count++;
                            oneSkuValueCode.attValue = $(this).val();
                            oneSkuValueCode.code = $(this).attr("id");
                            oneSkuValueCode.productcode = $(this).attr("name");
                            skuValueCode.push(oneSkuValueCode);
                            console.log(oneSkuValueCode)
                        } else {
                            count--;
                            for (var i = 0; i < skuValueCode.length; i++) {
                                if (skuValueCode[i].attValue == $(this).val()) {
                                    skuValueCode.remove(skuValueCode[i]);
                                }
                            }
                        }
                        el.previousElementSibling.innerHTML = count;//计算已选数量
                    });
                });

                //获取spu
                $("#spuusd input").each(function (i) {
                    $(this).click(function () {
                        var spuSkuValueCode = {};
                        $(this).toggleClass("btn-primary");
                        if ($(this).hasClass("btn-primary")) {
                            console.log($(this).attr("id"));
                            spuSkuValueCode.attValue = $(this).val();
                            spuSkuValueCode.code = $(this).attr("id");
                            spuValueCode.push(spuSkuValueCode);
                            console.log(spuSkuValueCode)
                        } else {
                            for (var i = 0; i < spuValueCode.length; i++) {
                                console.log(spuValueCode)
                                if (spuValueCode[i].attValue == $(this).val()) {
                                    spuValueCode.remove(spuValueCode[i]);
                                }
                            }
                        }

                    });
                });

            });


        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });

}


//确定
function resultfind() {
    var productName = th.parentElement.previousElementSibling.innerHTML;
console.log(productName)
    if(skuValueCode ==""&& spuValueCode==""){
        $.Huimodalalert('请选择详细信息！',2000);
        return false;
    }
    var objArr = [];
    if(skuValueCode !="" && skuValueCode != null){
        for (var i = 0; i < skuValueCode.length; i++) {
            var arraySku =  skuValueCode[i].code.split(",");
            var objJson = {
                "firstCategoryId":arraySku[1],
                "secondCategoryId":arraySku[2],
                "thirdCategoryId":arraySku[3],
                "firstCategoryName":arraySku[9],
                "secondCategoryName":arraySku[10],
                "thirdCategoryName":arraySku[4],
                "brandName": arraySku[6],
                "brandId":arraySku[5],
                "modifyPriceWay": '10',
                "specifications": skuValueCode[i].attValue,//规格名称
                "spuName":productName,//产品名称
                "spuCode": arraySku[7],
                "skuName": arraySku[8],//商品名称
                "skuCode": arraySku[0],
                "helpcode":arraySku[11],//助记码
                "unit": arraySku[12],//计量单位
                "skuId": arraySku[13],//计量单位


            };
            objArr.push(objJson);
        }

    }
    if(spuValueCode!="" && spuValueCode!=null) {
        for (var i = 0; i < spuValueCode.length; i++) {
            var arraySpu = spuValueCode[i].code.split(",");
            var objJson11 = {
                "firstCategoryId": arraySpu[0],
                "secondCategoryId": arraySpu[1],
                "thirdCategoryId": arraySpu[2],
                "firstCategoryName":arraySpu[8],
                "secondCategoryName":arraySpu[9],
                "thirdCategoryName": arraySpu[3],
                "brandName": arraySpu[5],
                "brandId": arraySpu[4],
                "modifyPriceWay": '10',
                "spuCode": arraySpu[6],
                "spuName": arraySpu[7],
                "skuCode": "",
                "helpCode": "",//助记码
                "unitName": "",//计量单位
                "skuId": "",//
                "skuName": "",//

            };

            objArr.push(objJson11);
        }
    }

    console.log(objArr);

    $.ajax({
        contentType: "application/json;charset=UTF-8",
        url:root.dockingplatform.addBatchPriceInfoRule,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data: JSON.stringify({
            "customerId": customerid2,
            "currentUserId": JSON.parse(web.readCookie("loginData")).user.user_ID,
            "customerPriceInfoRuleDtos": objArr

        }),
        success: function (data) {
            if(data.errorCode==0){
                window.parent.conterww();
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                $.Huimodalalert('修改成功！',2000);
            }else {
                $.Huimodalalert(data.msg,2000);
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })
}



//取消
function guanss() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
    layer.msg('已取消', {icon: 6,time:1000});
}

